package com.example.controller;

import com.example.service.FruitCleanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@RestController
@Validated
@RequestMapping("/")
public class FruitCleanController {
    @Autowired
    private FruitCleanService fruitCleanService;

    // 生成指定关卡
    @RequestMapping("/start-level")
    public String generatorGate(@RequestParam("level") Integer level) {
        return fruitCleanService.generatorGate(level);
    }

    // 移动
    @GetMapping("/move")
    public String move(
            @RequestParam("sessionId")String sessionId,
            @Min(value = 0)@Max (value = 7)@RequestParam("row0") Integer row0,
            @Min(value = 0)@Max (value = 7)@RequestParam("col0") Integer col0,
            @Min(value = 0)@Max (value = 7)@RequestParam("row1") Integer row1,
            @Min(value = 0)@Max (value = 7)@RequestParam("col1") Integer col1) {
        if (row0 > row1 || col0 > col1) {
            throw new ValidationException();
        }
        return fruitCleanService.move(sessionId,row0,col0,row1,col1);
    }
}
